package com.example.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: jiawp
 * @date: 2024/1/23 7:30
 * @description:
 **/
public class Main {

    public static void main(String[] args) {
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("张三", 20));
        persons.add(new Person("李四", 25));
        persons.add(new Person("张三", 22));
        persons.add(new Person("王五", 30));
        persons.add(new Person("李四", 28));

        Map<String, Integer> maxAgeByName = getMaxAgeByName(persons);
        for (Map.Entry<String, Integer> entry : maxAgeByName.entrySet()) {
            System.out.println("姓名： " + entry.getKey() + ", 最大年龄： " + entry.getValue());
        }
    }

    public static Map<String, Integer> getMaxAgeByName(List<Person> persons) {
        Map<String, Integer> maxAgeByName = new HashMap<>();
        for (Person person : persons) {
            maxAgeByName.put(person.name, Math.max(person.age, maxAgeByName.getOrDefault(person.name, person.age)));
        }
        return maxAgeByName;
    }
}

class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}


